﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.ViewModel;
using System.Web.Mvc;

// add by chenqiao 2013/7/21 17:28 
namespace XSTDZ.ERP.WebApp.Models
{
    /// <summary>
    /// 组织机构类
    /// </summary>
    public class OrgModel
    {
        /// <summary>
        /// 初始化根结点
        /// </summary>
        /// <returns></returns>
        public OrgDto InitOrgRoot()
        {
            OrgDto org = new OrgDto
            {
                Id = Guid.NewGuid(),
                Level=0,
                Name = XSTDZ.ERP.Infrastructure.SystemConfig.SystemName
            };
            org.Code = org.Name;
            return org;
        }
        /// <summary>
        /// 递归所有组织机构和部门，返回给前台的liguer树控件
        /// </summary>
        /// <param name="orglst"></param>
        /// <param name="deptlst"></param>
        /// <returns></returns>
        public OrgViewModel GetAllOrgDept(IEnumerable<OrgDto> orglst, IEnumerable<DeptDto> deptlst)
        {
            if (orglst == null)
            {
                return null;
            }
            var root = orglst.Where(o => o.Level == 1).First();
            OrgViewModel orgTree = new OrgViewModel { 
                 id=root.Id.ToString(),
                  text=root.Name,
                   isorg="1"
            };
            OrgViewChildren(orglst, deptlst,1, orgTree);
            return orgTree;
        }
        #region 组织机构递归
        //组织机构递归
        private void OrgViewChildren(IEnumerable<OrgDto> orglst,IEnumerable<DeptDto> deptlst,short level,OrgViewModel parent)
        {
            
            var parentLevelIds = orglst.Where(o => o.Level ==(short)(level+1)).Select(o => o.Id).Distinct();
            foreach (var id in parentLevelIds)
            {
                var item = orglst.Where(o => o.Id == id);
                Add(parent, item, orglst, deptlst);
            }
        }
        private void Add(OrgViewModel orgview, IEnumerable<OrgDto> children, IEnumerable<OrgDto> orglst, IEnumerable<DeptDto> deptlst)
        {
            
            if (children != null)
            {
                short level=(short)children.First().Level;
                foreach (var item in children)
                {
                    var orgviewchild = new OrgViewModel()
                    {
                        id = item.Id.ToString(),
                        text = item.Name.ToString(),
                        isorg="1",
                        icon = @XSTDZ.ERP.Infrastructure.SystemConfig.orgIcon
                    };
                    orgview.children.Add(orgviewchild);
                    //部门循环
                    if (deptlst != null)
                    {
                        DeptViewChildren(deptlst, 0, orgviewchild);
                    }

                    OrgViewChildren(orglst,deptlst, level, orgviewchild);
                }
            }
             
        }
        #endregion
        #region 部门递归
        
        
        //部门递归
        private void DeptViewChildren(IEnumerable<DeptDto> deptlst, short level, OrgViewModel parent)
        {
            var parentLevelIds =new List<Guid>(0);
            if (level == 0)
            {
                parentLevelIds = deptlst.Where(o => o.Level == (short)(level + 1) && o.OrgId == Guid.Parse(parent.id)).Select(o => o.Id).Distinct().ToList();
            }
            else
            {
                parentLevelIds = deptlst.Where(o => o.Level == (short)(level + 1) && o.ParentId == Guid.Parse(parent.id)).Select(o => o.Id).Distinct().ToList();
            }
            foreach (var id in parentLevelIds)
            {
                var item = deptlst.Where(o => o.Id == id).ToList();
                if (item != null && item.Count > 0)
                {
                    AddDept(parent, item, deptlst);
                }
            }
        }
        private void AddDept(OrgViewModel deptview, IEnumerable<DeptDto> children, IEnumerable<DeptDto> deptlst)
        {

            if (children != null)
            {
                short level = (short)children.First().Level;
                foreach (var item in children)
                {
                    var deptviewchild = new OrgViewModel()
                    {
                        id = item.Id.ToString(),
                        text = item.Name.ToString(),
                        isorg = "0",
                        icon = @XSTDZ.ERP.Infrastructure.SystemConfig.deptIcon
                    };
                    deptview.children.Add(deptviewchild);
                    DeptViewChildren(deptlst, level, deptviewchild);
                }
            }

        }

        #endregion



    }
       
}